<?php
/**
 * Created by PhpStorm.
 * User: sunny
 * Date: 2019/2/27
 * Time: 10:37
 */
require_once ('common.php');
//添加测试白名单
$testwhitelist_url = 'https://api.weixin.qq.com/card/testwhitelist/set?access_token=';

//1.获取access_tokenn
$access_token = get_access_token();
$data = $_POST;
if(empty($data)){
    echo_Json(-1, '数据不能为空');
}
$openidStr   = $data['openid'];             //openid,多个openid以逗号分隔
$usernameStr = $data['username'];           //微信号username,多个username以逗号分隔
if(empty($openidStr) && empty($usernameStr)){
    echo_Json(-1, 'openid和username不能同时为空');
}
/*------------------组装数据-------------*/
if($openidStr && empty($usernameStr)){
    $openidArr   = explode(',', $openidStr);
    $postData = array(
        'openid' => $openidArr
    );
}elseif($usernameStr && empty($openidStr)){
    $usernameArr = explode(',', $usernameStr);
    $postData = array(
        'username' => $usernameArr
    );
}else{
    $openidArr   = explode(',', $openidStr);
    $usernameArr = explode(',', $usernameStr);
    $postData = array(
        'openid' => $openidArr,
        'username' => $usernameArr
    );
}
/*------------------组装数据-------------*/
// $postData = array(
//     'openid' => array(
//         0 =>'oEZwf1IRZc5a9EK-qK7kkUJLe5dw', //wkk
//         1=> 'oEZwf1K3_44Mx6TMVDYf6ModD6xs'  //trm
//     )
// );
//添加白名单
$result = addtestwhitelist($testwhitelist_url.$access_token, $postData);
if($result['code'] != 0){
    echo_Json($result['code'], '添加白名单失败');
}else{
    echo_Json(0, '添加白名单成功');
}
function addtestwhitelist($url, $data){
    $addresultArr = jsonpostJson($url, $data, true);
    if($addresultArr['errcode'] != 0){
        $result = array(
            'code' => $addresultArr['errcode'],
        );
    }else{
        $result = array(
            'code' => 0
        );
    }
    return $result;
}